package com.example.petadopt.controller;

import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.example.petadopt.controller.vo.Result;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/oss")
public class OssController {
    private OSS ossClient;
    private static final String BUCKET_NAME = "hp-link";//你在oss定的BUCKET_NAME
    private static final String FOLDER_NAME = "imgs/";

    public OssController() {
        try {
            ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
            clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
            this.ossClient = OSSClientBuilder.create()
                    .endpoint("https://oss-cn-guangzhou.aliyuncs.com")
                    .region("cn-guangzhou")
                    .credentialsProvider(CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider())
                    .clientConfiguration(clientBuilderConfiguration)
                    .build();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @PostMapping("/upload")
    public Result upload(@RequestParam("files") MultipartFile[] files) {
        List<String> urls = new ArrayList<>();
        for (MultipartFile file : files) {
            try (InputStream inputStream = file.getInputStream()) {
                // 生成文件名，添加文件夹前缀
                String objectName = FOLDER_NAME + new Date().getTime() + "_" + file.getOriginalFilename();
                PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET_NAME, objectName, inputStream);
                PutObjectResult result = ossClient.putObject(putObjectRequest);
                // 构建文件访问URL
                String fileUrl = "https://" + BUCKET_NAME + ".oss-cn-guangzhou.aliyuncs.com/" + objectName;
                urls.add(fileUrl);
            } catch (IOException e) {
                // 记录日志并返回详细的错误信息
                e.printStackTrace();
                return new Result(5000, "上传失败: " + e.getMessage(), null);
            }
        }
        return new Result(2000, "上传成功", urls);
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody Map<String, String[]> requestBody) {
        String[] objectNames = requestBody.get("objectNames");
        if (objectNames == null) {
            return new Result(5000, "删除失败: 参数 objectNames 不能为空", null);
        }
        for (String objectName : objectNames) {
            try {
                // 添加文件夹前缀
                String fullObjectName = FOLDER_NAME + objectName;
                ossClient.deleteObject(BUCKET_NAME, fullObjectName);
            } catch (Exception e) {
                // 记录日志并返回详细的错误信息
                e.printStackTrace();
                return new Result(5000, "删除失败: " + e.getMessage(), null);
            }
        }
        return new Result(2000, "删除成功", null);
    }
}